package com.haizhi.crm.api.controller.customer;


import com.haizhi.crm.biz.constant.AccountType;
import com.haizhi.crm.biz.constant.CompanyBasicType;
import com.haizhi.crm.biz.model.*;
import com.haizhi.crm.biz.service.BrowseCountService;
import com.haizhi.crm.biz.service.CompanyService;
import com.haizhi.crm.biz.service.EventService;
import com.haizhi.crm.biz.service.VisitRecordService;
import com.haizhi.crm.common.constant.CrmSchema;
import com.haizhi.crm.common.model.Response;
import com.haizhi.crm.common.util.Getter;
import com.haizhi.crm.tag.model.TagQo;
import com.haizhi.crm.tag.model.TagResp;
import com.haizhi.crm.tag.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * Desc:客户信息
 * Created by yangyijun on 2018/6/5.
 */
@Api(description = "[业务-客户]" )
@RestController
@RequestMapping("/customer" )
public class CustomerController {

    @Autowired
    private CompanyService companyService;

    @Autowired
    private EventService eventService;
    @Autowired
    private TagService tagService;

    @Autowired
    private BrowseCountService browseCountService;


    @Autowired
    private VisitRecordService visitRecordService;

    @ApiOperation(value = "客户详情页-查询行内动态列表", notes = "例：{\"customerKey\": \"7908891a00d02b29354c4dd5147de439\", \"eventType\": \"all\", \"pageNo\":1, \"pageSize\": 10 }\n" +
            "根据客户company_key(7908891a00d02b29354c4dd5147de439)和事件类型（evenType）在ES的tv_inner_event中查找；" +
            "事件类型有：all 全部 | account 账户变动 |  business 业务到期 | performance 业绩关系变化 | attribution 归属关系变动 | risk 风险评级变化 " )
    @PostMapping("/findInnerEvents" )
    public Response findInnerEvents(@RequestBody @Valid CustomerEventQo customerEventQo) {
        return eventService.findInnerEvents(customerEventQo);
    }

    @ApiOperation(value = "客户详情页-查询拜访记录列表", notes = "{\"customerKey\": \"7908891a00d02b29354c4dd5147de439\", \"eventType\": \"all\", \"pageNo\":1, \"pageSize\": 10 }" )
    @PostMapping("/findVisitRecords" )
    public Response findVisitRecords(@RequestBody @Valid CustomerVisitRecordQo customerVisitRecordQo) {
        return visitRecordService.findByCustomerKey(customerVisitRecordQo);
    }

    @ApiOperation(value = "客户详情页-查询行外事件列表", notes = "例：{\"customerKey\": \"7908891a00d02b29354c4dd5147de439\", \"eventType\": \"all\", \"pageNo\":1, \"pageSize\": 10 }\n" +
            "根据客户company_key(7908891a00d02b29354c4dd5147de439)和事件类型（evenType）在ES的tv_outer_event中查找；" +
            "事件类型有：all 全部 | enterprise 工商信息变更 |  bid 招中标 | law 涉诉信息 | taxes 欠税公告 | adm 行政处罚 | news 新闻舆情 " )
    @PostMapping("/findOuterEvents" )
    public Response findOuterEvents(@RequestBody @Valid CustomerEventQo customerEventQo) {
        return eventService.findOuterEvents(customerEventQo);
    }


    @ApiOperation(value = "为搜索输入框提供最优候选项", notes =
            "    stock_structure(\"股权结构\"),\n" +
                    "    invest(\"投资对象\"),\n" +
                    "    officer(\"人员构成\"),\n" +
                    "    guarantee(\"担保关系\"),\n" +
                    "    capital_transfer(\"资金往来\"),\n" +
                    "    concert(\"一致行动人\"),\n" +
                    "    actual_controller(\"实际控制人\")" +
                    "{ id : 'aaa', entity_type : 'Company', graph_type_list : ['invest','guarantee'] }" )
    @RequestMapping(
            path = "relationship",
            method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public Response getRelationship(@RequestBody RelationshipQo relationshipQo) {
        try {
            Object resp = this.companyService.getRelationship(relationshipQo);
            return Response.success().setData(resp);
        } catch (Exception e) {
            return Response.error();
        }
    }

    @ApiOperation(value = "客户详情页-获取企业已开展业务-产品签约", notes = "{\n" +
            "  \"companyKey\": \"7908891a00d02b29354c4dd5147de439\",\n" +
            "  \"detailType\": \"投资类\",\n" +
            "\"productName\":\"活期 存款\",\n" +
            "  \"pageNo\": 1,\n" +
            "  \"pageSize\": 10\n" +
            "}" )
    @PostMapping(path = "findProductSigns" )
    public Response findProductSigns(@RequestBody @Valid CompanyProductSignQo companyStartServiceQo) {
        return companyService.findProductSigns(companyStartServiceQo);
    }

    @ApiOperation(value = "获取企业基本信息-标签", notes = "?id=7908891a00d02b29354c4dd5147de439" )
    @GetMapping(path = "basic/tag" )
    public Response getTag(@RequestParam String id) {
        try {
            TagQo tagQo = new TagQo();
            tagQo.setGraph(CrmSchema.CRM);
            tagQo.addObjectKeys(id);
            TagResp tagResp = tagService.findByObjectKeys(tagQo);
            Map<String, Object> map = Getter.getMap(tagResp.getPayload());
            if (!CollectionUtils.isEmpty(map) && !CollectionUtils.isEmpty(map.values())) {
                tagResp.setPayload(map.values().iterator().next());
            }
            return Response.success(tagResp);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error();
        }
    }

    @ApiOperation(value = "获取企业基本信息-概览信息", notes = "?company_id=7908891a00d02b29354c4dd5147de439" )
    @RequestMapping(
            path = "basic/general",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public Response getGeneral(@RequestParam("company_id" ) String id) {
        try {
            browseCountService.count(CrmSchema.COMPANY, id);
            Object resp = this.companyService.getBasic(id, CompanyBasicType.GENERAL);
            return Response.success().setData(resp);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error();
        }
    }


    @ApiOperation(value = "获取企业基本信息-注册信息", notes = "?company_id=7908891a00d02b29354c4dd5147de439" )
    @RequestMapping(
            path = "basic/registration",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public Response getRegistration(@RequestParam("company_id" ) String id) {
        try {
            Object resp = this.companyService.getBasic(id, CompanyBasicType.REGISTRATION);
            return Response.success().setData(resp);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error();
        }
    }

    @ApiOperation(value = "获取企业基本信息-管理信息", notes = "?company_id=7908891a00d02b29354c4dd5147de439" )
    @RequestMapping(
            path = "basic/manager",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public Response getManager(@RequestParam("company_id" ) String id) {
        try {
            Object resp = this.companyService.getBasic(id, CompanyBasicType.MANAGER);
            return Response.success().setData(resp);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error();
        }
    }

    @ApiOperation(value = "获取企业基本信息-证件信息", notes = "?company_id=7908891a00d02b29354c4dd5147de439" )
    @RequestMapping(
            path = "basic/license",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public Response getLicense(@RequestParam("company_id" ) String id) {
        try {
            Object resp = this.companyService.getBasic(id, CompanyBasicType.LICENSE);
            return Response.success().setData(resp);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error();
        }
    }

    @ApiOperation(value = "获取企业基本信息-征信信息", notes = "?company_id=7908891a00d02b29354c4dd5147de439" )
    @RequestMapping(
            path = "basic/credit",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public Response getCredit(@RequestParam("company_id" ) String id) {
        try {
            Object resp = this.companyService.getBasic(id, CompanyBasicType.CREDIT);
            return Response.success().setData(resp);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error();
        }
    }

    @ApiOperation(value = "获取企业账户总览", notes = "?companyName" )
    @GetMapping("basic/account" )
    public Response getAccount(@RequestParam("companyKey" ) String companyKey, @RequestParam("accountType" ) AccountType accountType) {
        try {
            Object resp = this.companyService.getAccount(companyKey, accountType);
            return Response.success().setData(resp);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error();
        }
    }

    @ApiOperation(value = "获取企业账户转账明细", notes = "?companyName" )
    @RequestMapping(
            path = "transfer/detail",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public Response getTransfer(@RequestParam("accountNo" ) String accountNo) {
        try {
            Object resp = this.companyService.getTransfer(accountNo);
            return Response.success().setData(resp);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error();
        }
    }

    @ApiOperation(value = "根据公司名查询公司", notes = "ES的crm.company.name索引中查找" )
    @GetMapping("/findByName" )
    public Response findByName(@RequestParam String customerName) {
        return this.companyService.findByName(customerName);
    }

    @ApiOperation(value = "客户列表-搜索客户列表", notes = "{ \"customerSource\": \"all\", \"customerType\": \"my\", \"pageNo\": 1,  \"pageSize\": 10}" )
    @PostMapping("/search" )
    public Response search(@RequestBody @Valid SearchCustomerReqQo searchCustomerReqQo) {
        return companyService.searchCustomers(searchCustomerReqQo);
    }

    @ApiOperation(value = "客户列表-高级搜索客户列表", notes = "{\"isListedCompany\":\"True\", \"pageNo\": 1,  \"pageSize\": 10}" )
    @PostMapping("/advancedSearch" )
    public Response advancedSearch(@RequestBody @Valid CompanyAdvancedSearchQo companyAdvancedSearchQo) {
        return companyService.advancedSearch(companyAdvancedSearchQo);
    }

}
